package jeecg.business.controller.send;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jeecg.business.entity.send.SendMessEntity;
import jeecg.business.service.send.SendMessEnterServiceI;
import jeecg.business.service.send.SendMessServiceI;
import jeecg.system.service.SystemService;

import org.apache.log4j.Logger;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**   
 * @Title: Controller
 * @Description: 信息推送
 * @author LinYingPiaoJian
 * @date 2015-07-22 16:48:27
 * @version V1.0   
 *
 */
@Controller
@RequestMapping("/sendMessController")
public class SendMessController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(SendMessController.class);

	@Autowired
	private SendMessServiceI sendMessService;
	@Autowired
	private SystemService systemService;
	@Autowired
	private SendMessEnterServiceI sendMessEnterService;
	
	private String message;
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}


	/**
	 * 信息推送列表 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "sendMess")
	public ModelAndView sendMess(HttpServletRequest request) {
		return new ModelAndView("jeecg/business/send/sendMessList");
	}

	/**
	 * easyui AJAX请求数据
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param user
	 */
	@RequestMapping(params = "datagrid")
	public void datagrid(SendMessEntity sendMess,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(SendMessEntity.class, dataGrid);
		//查询条件组装器
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, sendMess);
		this.sendMessService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}

	
	
	
	@RequestMapping(params = "send")
	@ResponseBody
	public AjaxJson send(SendMessEntity sendMess, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		sendMess = systemService.getEntity(SendMessEntity.class, sendMess.getId());
		message = "推送成功";
		boolean cando;
		try {
			cando = sendMessService.send(sendMess);
			if(!cando) message = "未配置推送单位";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			 message = "推送失败";
		}
		
		j.setMsg(message);
		return j;
	}
	/**
	 * 删除信息推送
	 * 
	 * @return
	 */
	@RequestMapping(params = "del")
	@ResponseBody
	public AjaxJson del(SendMessEntity sendMess, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		sendMess = systemService.getEntity(SendMessEntity.class, sendMess.getId());
		message = "删除成功";
		sendMessService.delete(sendMess);
		systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
		
		j.setMsg(message);
		return j;
	}


	/**
	 * 添加信息推送
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "save")
	@ResponseBody
	public AjaxJson save(SendMessEntity sendMess, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		if (StringUtil.isNotEmpty(sendMess.getId())) {
			message = "更新成功";
			SendMessEntity t = sendMessService.get(SendMessEntity.class, sendMess.getId());
			try {
				MyBeanUtils.copyBeanNotNull2Bean(sendMess, t);
				sendMessService.saveOrUpdate(t);
				systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			message = "添加成功";
			sendMess.setZt("1");
			sendMessService.save(sendMess);
			systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
		}
		
		return j;
	}

	/**
	 * 信息推送列表页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "addorupdate")
	public ModelAndView addorupdate(SendMessEntity sendMess, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(sendMess.getId())) {
			sendMess = sendMessService.getEntity(SendMessEntity.class, sendMess.getId());
			sendMess.setNames(sendMessEnterService.getSelectName(sendMess.getId()));
		}else{
			sendMess = new SendMessEntity();
			sendMess.setYjfs("1");
			sendMess.setDxfs("1");
			sendMess.setBjry(getUser().getRealName());
			sendMess.setCjsj(new Date());
		}
		req.setAttribute("sendMessPage", sendMess);
		return new ModelAndView("jeecg/business/send/sendMess");
	}
}
